package com.jkozniewski.util.util3d.pv3d
{
	
	import flash.geom.*;
	
	import org.papervision3d.core.proto.CameraObject3D;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.view.Viewport3D;
	
	public class PV3DUtils {
		
		
		public static function getRectFromCoords( x:Number, y:Number, z:Number, 
												  w:Number, h:Number, 
												  camera:CameraObject3D, viewport:Viewport3D ) : Rectangle {
			
			var n34:Number = -camera.z + z;
			var persp:Number = (camera.focus * camera.zoom) / (camera.focus + n34);
			
			x *= persp;
			y *= -persp;
			
			w *= persp;
			h *= persp;
			
			x += viewport.width/2;
			y += viewport.height/2;
			
			
			return new Rectangle( x, y, w, h );	
			
		}
		
		public static function setScreenCoords( do3d:DisplayObject3D, camera:CameraObject3D, x:Number, y:Number ) : void {
			
			var n34:Number = -camera.z + do3d.z;
			var persp:Number = (camera.focus * camera.zoom) / (camera.focus + n34);
			
			do3d.x = x / persp; 
			do3d.y = -y / persp;
			
		}
		
		public static function getXYCoordBounds( viewport:Viewport3D, camera:CameraObject3D ) : Rectangle {
			
			
			
			var rect:Rectangle = viewport.getRect( viewport );
			rect.x -= viewport.width/2;
			rect.y -= viewport.height/2;
			rect.right += viewport.width/2;
			rect.bottom += viewport.width/2;
			
			var persp:Number = camera.zoom //(camera.focus * camera.zoom) / (camera.focus);
			
			rect.x *= persp;
			rect.y *= persp;
			rect.bottom *= persp;
			rect.right *= persp;
			
			//rect.
			
			trace( "viewportRect", rect.x, rect.y, rect.right, rect.bottom, persp );
			
			return rect;
			
		}
		
		
		
		
		

	}
}